<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务编号" prop="taskId">
        <el-input v-model="queryParams.taskId" placeholder="请输入任务编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="图片" prop="picture">
        <el-input v-model="queryParams.picture" placeholder="请输入图片" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="推送开关" prop="pushSwitch">
        <el-input v-model="queryParams.pushSwitch" placeholder="请输入推送开关" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="推送条件" prop="pushCondition">
        <el-input v-model="queryParams.pushCondition" placeholder="请输入推送条件" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:push:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:push:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:push:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:push:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddPush"
          v-hasPermi="['system:push:add']">推送消息</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pushList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务编号" align="center" prop="taskId" />
      <el-table-column label="推送内容" align="center" prop="pushContent" />
      <el-table-column label="推送用户类型" align="center" prop="pushUserType" />
      <el-table-column label="推送用户" align="center" prop="pushUser" />
      <!-- <el-table-column label="图片" align="center" prop="picture" /> -->
      <el-table-column label="推送开关" align="center" prop="pushSwitch" />
      <el-table-column label="推送条件" align="center" prop="pushCondition" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:push:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:push:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" :visible.sync="openPush" width="500px" append-to-body>
      <el-form ref="formPush" :model="formPush" label-width="80px">
        <el-form-item label="推送内容">
          <el-input v-model="formPush.pushContent" type="textarea" placeholder="请输入推送内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormPush">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改消息推送对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="推送内容">
          <el-input v-model="form.pushContent" type="textarea" placeholder="请输入推送内容" />
        </el-form-item>
        <el-form-item label="推送用户类型" prop="pushUserType">
          <el-select v-model="form.pushUserType" placeholder="推送用户类型" @change="changeUserType">
            <el-option v-for="item in pushUserTypeOption" :key="item.value" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.pushUserType == 2" label="推送用户" prop="pushUser">
          <el-input v-model="form.pushUser" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="推送开关" prop="pushSwitch">
          <el-select v-model="form.pushSwitch" placeholder="推送开关">
            <el-option v-for="item in pushSwitch" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="推送条件" prop="pushCondition">
          <el-select v-model="form.pushCondition" placeholder="请选择天数">
            <el-option v-for="item in dayOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPush, getPush, delPush, addPush, updatePush, pushAll } from "@/api/system/push";

export default {
  name: "Push",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息推送表格数据
      pushList: [],
      //天数列表
      dayOption: [{ "label": '1天', "value": 1 }, { "label": '2天', "value": 2 }, { "label": '3天', "value": 3 }],
      //推送条件是否
      pushSwitch: [{ "label": '是', "value": 1 }, { "label": '否', "value": 0 }],
      //推送用户类型
      pushUserTypeOption: [{ "label": '全部用户', "value": 1 }, { "label": '部分用户', "value": 2 }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示推送
      openPush: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        pushContent: null,
        pushUserType: null,
        pushUser: null,
        picture: null,
        pushSwitch: null,
        pushCondition: null,
        yn: null,
      },
      // 表单参数
      form: {},
      formPush: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询消息推送列表 */
    getList() {
      this.loading = true;
      listPush(this.queryParams).then(response => {
        console.log('response',response);
        response.rows.forEach(item => {
          item.pushUserType = item.pushUserType == '1' ? '全部用户' : '部分用户'
          item.pushSwitch = item.pushSwitch == '1' ? '开' : '关'
          if(item.pushCondition == 1){
            item.pushCondition = '1天'
          }else if(item.pushCondition == 2){
            item.pushCondition = '2天'
          }else if(item.pushCondition == 3){
            item.pushCondition = '3天'
          }else{
            item.pushCondition = '-'
          }
        });
        this.pushList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskId: null,
        pushContent: null,
        pushUserType: null,
        pushUser: null,
        picture: null,
        pushSwitch: null,
        pushCondition: null,
        yn: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消息推送";
    },
    handleAddPush() {
      this.openPush = true;
      this.title = "消息推送";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPush(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息推送";
      });
    },
    changeUserType(row) {

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePush(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPush(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitFormPush() {
      var param = {};
      param.msg = this.formPush.pushContent;
      pushAll(param).then(response => {
        debugger;
        this.$modal.msgSuccess("发送成功！");
        this.openPush = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除消息推送编号为"' + ids + '"的数据项？').then(function () {
        return delPush(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/push/export', {
        ...this.queryParams
      }, `push_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
